package tx.yt.charge.service;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import tx.yt.charge.model.UserInfo;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: chenmiao@hupu.com
 * Date: 2015-1-8
 */
@Service
public class UserService {

    @Resource
    JdbcTemplate jdbcTemplate;

    public UserInfo validateUser(final String username, final String password){
        String sql = "select * from userinfo where accountName = ? and password = ?";
        List<UserInfo> result = jdbcTemplate.query(sql,new Object[]{username,password},new RowMapper<UserInfo>() {
            @Override
            public UserInfo mapRow(ResultSet rs,int rowNum) throws SQLException {
                UserInfo userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setCustomerId(rs.getString("customerId"));
                userInfo.setAccountName(rs.getString("accountName"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setLeader(rs.getString("leader"));
                userInfo.setCertificateType(rs.getString("certificateType"));
                userInfo.setCertificateNo(rs.getString("certificateNo"));
                userInfo.setPhone(rs.getString("phone"));
                userInfo.setMobile(rs.getString("mobile"));
                userInfo.setEmail(rs.getString("email"));
                userInfo.setAddress(rs.getString("address"));
                userInfo.setQq(rs.getString("qq"));
                userInfo.setDepartementId(rs.getString("departementId"));
                userInfo.setCreateTime(rs.getTimestamp("createTime"));
                userInfo.setModifyTime(rs.getTimestamp("modifyTime"));
                return userInfo;
            }
        });
        if(result.isEmpty())
            return null;
        return result.get(0);
    }
}
